package com.codeoss.framework.data.metadata;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 数据库表 元数据
 * @author nethaoke
 * 2020-06-03
 */
public class Table {
	private static final Logger logger = LoggerFactory.getLogger(Table.class);
	
	private String tableName;
	private String name;
	private List<Column> columnList=new ArrayList<Column>();
	private Column primaryKeyColumn;
	private Column parentColumn;
	private String packageName;
	//在导入模块时  是否自动创建表  视图和base.sql导入的表不要自动创建
	private boolean auto=true;
	//视图
	private boolean view;
	private boolean abstractTable;
	private String extendsTable;
	private Map<String,Column> columnMap=new HashMap<String,Column>();
	
	private Map<String,Column> propertyColumnMap=new HashMap<String,Column>();
	
	private List<Column> allColumnList=new ArrayList<Column>();
	private Table superTable;
	
	private List<TableValidationRule> tableValidationRuleList;
	
	private Column codeColumn;
	private Column nameColumn;
	
	private TableCRUDRule tableCRUDRule;
	
	public static final String ACCESSTYPE_MYBATIS="mybatis";
	public static final String ACCESSTYPE_DB="db";
	public static final String ACCESSTYPE_REST="rest";
	
	private String accessType=ACCESSTYPE_MYBATIS;
	private String sql;
	private List<Column> loggingColumnList=new ArrayList<Column>();
	public String getTableName() {
		return tableName;
	}
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public Column getPrimaryKeyColumn() {
		return primaryKeyColumn;
	}
	public void setPrimaryKeyColumn(Column primaryColumn) {
		this.primaryKeyColumn = primaryColumn;
	}
	public void addColumn(Column column) {
		columnList.add(column);
	}
	public List<Column> getColumnList() {
		return columnList;
	}
	public void setColumnList(List<Column> columnList) {
		this.columnList = columnList;
	}
	
	
	public String getPackageName() {
		return packageName;
	}
	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}
	public boolean isAuto() {
		return auto;
	}
	public void setAuto(boolean auto) {
		this.auto = auto;
	}
	public boolean isView() {
		return view;
	}
	public void setView(boolean view) {
		this.view = view;
	}
	
	public boolean isAbstractTable() {
		return abstractTable;
	}
	public void setAbstractTable(boolean abstractTable) {
		this.abstractTable = abstractTable;
	}
	
	public String getExtendsTable() {
		return extendsTable;
	}
	public void setExtendsTable(String extendsTable) {
		this.extendsTable = extendsTable;
	}	
	
	public Table getSuperTable() {
		return superTable;
	}
	public void setSuperTable(Table superTable) {
		this.superTable = superTable;
	}
	public Column getColumnByColumnName(String columnName) {
		Column column=columnMap.get(columnName);
		if(column==null) {
			logger.warn("表"+tableName+"不存在列"+columnName);
		}
		return column;
	}
	
	public Column getColumnByPropertyName(String property) {
		Column column=propertyColumnMap.get(property);
		if(column==null) {
			logger.warn("表"+tableName+"不存在列"+property);
		}
		return column;
	}
	
	public List<Column> getAllColumnList() {
		return allColumnList;
	}
	public void setAllColumnList(List<Column> allColumnList) {
		this.allColumnList = allColumnList;
	}
	
	public void build() {
		for(Column column:allColumnList) {
			 if(column.isPrimaryKey()) {
				 primaryKeyColumn=column;
			 }else if(column.isParentColumn()) {
				 parentColumn=column;
			 }else if(column.isCodeColumn()) {
				 codeColumn=column;
			 }else if(column.isNameColumn()) {
				 nameColumn=column;
			 }else if(column.isAllowLogging()) {
				 loggingColumnList.add(column);
			 }
			 columnMap.put(column.getColumnName(), column);
			 propertyColumnMap.put(column.getProperty(), column);
		}
		if(isView() ||isAbstractTable()) {
			auto=false;
		}
	}
	
	public Column getParentColumn() {
		return parentColumn;
	}
	public void setParentColumn(Column parentColumn) {
		this.parentColumn = parentColumn;
	}
	
	public Column getCodeColumn() {
		return codeColumn;
	}
	public void setCodeColumn(Column codeColumn) {
		this.codeColumn = codeColumn;
	}
	public Column getNameColumn() {
		return nameColumn;
	}
	public void setNameColumn(Column nameColumn) {
		this.nameColumn = nameColumn;
	}
	public boolean isTreeTable() {
		if("base_tree".equals(extendsTable)) {
			return true;
		}
		return false;
	}
	
	public List<TableValidationRule> getTableValidationRuleList() {
		return tableValidationRuleList;
	}
	
	public void setTableValidationRuleList(List<TableValidationRule> tableValidationRuleList) {
		this.tableValidationRuleList = tableValidationRuleList;
	}
	
	public TableCRUDRule getTableCRUDRule() {
		return tableCRUDRule;
	}
	public void setTableCRUDRule(TableCRUDRule tableCRUDRule) {
		this.tableCRUDRule = tableCRUDRule;
	}
	public String getAccessType() {
		return accessType;
	}
	public void setAccessType(String accessType) {
		this.accessType = accessType;
	}
	public String getSql() {
		return sql;
	}
	public void setSql(String sql) {
		this.sql = sql;
	}
	
	public List<Column> getLoggingColumnList(){
		return loggingColumnList;
	}
}
